package com.fbty.mt.action.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;

import com.fbty.base.action.BaseAction;
import com.fbty.mt.utils.OnlyIPTxt;
import com.fbty.mt.utils.StaticValue;

public class FileDownAction extends BaseAction {
	private String fileName;
	
	private InputStream fileStream;
	
	public String execute(){
		File f = new File("error.txt");
		try {
			if(!f.exists()){
				f.createNewFile();
			}
			//将上传失败的ip输出到文件，然后提供下载
			PrintWriter out = new PrintWriter(f);
			Iterator<String> it = StaticValue.errrorIp.iterator();
			while(it.hasNext()){
				out.println(it.next());
				it.remove();
			}
			out.close();
			fileStream = new FileInputStream(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		fileName =System.currentTimeMillis()+"error.txt";
		return "success";
	}
	
	public String onlyError(){
		File f = new File("error.txt");
		try {
			if(!f.exists()){
				f.createNewFile();
			}
			//将上传失败的ip输出到文件，然后提供下载
			PrintWriter out = new PrintWriter(f);
			Iterator<OnlyIPTxt>	it = StaticValue.errorOnlyTxt.iterator();
			while(it.hasNext()){
				out.println(it.next());
				it.remove();
			}
			out.close();
			fileStream = new FileInputStream(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		fileName =System.currentTimeMillis()+"error.txt";
		return "success";
	}
	
	public InputStream getInputStream() throws Exception {
		return fileStream;
	}

	public String getFileName() {
		return fileName;
	}
	
}
